package com.metadata_system.api_server.system_server;

import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.service.DatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/system-server/v001/Database/")
@RestController("systemDatabaseController")
public class DatabaseController {

    @Autowired
    private DatabaseService databaseService;
    @Autowired
    LoginUserRequest loginUserRequest;

    @CheckTokenUser
    @RequestMapping(value = "getList", method = {RequestMethod.POST, RequestMethod.GET})
    public ApiResult getList() {
        return databaseService.getList(loginUserRequest.getTokenUser().decodeID());
    }

    @CheckTokenUser
    @RequestMapping(value = "create", method = {RequestMethod.POST, RequestMethod.GET})
    public ApiResult create(
            @RequestParam("name") String name,
            @RequestParam("type") Integer type) {
        return databaseService.create(name, type, loginUserRequest.getTokenUser().decodeID());
    }


    @CheckTokenUser
    @RequestMapping(value = "update", method = {RequestMethod.POST, RequestMethod.GET})
    public ApiResult update(
            @RequestParam("name") String name,
            @RequestParam("id") Integer id) {
        return databaseService.update(id, name, loginUserRequest.getTokenUser().decodeID());
    }


    @CheckTokenUser
    @RequestMapping(value = "delete", method = {RequestMethod.POST, RequestMethod.GET})
    public ApiResult delete(@RequestParam("id") Integer id) {
        return databaseService.delete(id, loginUserRequest.getTokenUser().decodeID());
    }


}
